package com.ybl.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybl.common.R;
import com.ybl.dto.DishDto;
import com.ybl.entity.Category;
import com.ybl.entity.Dish;
import com.ybl.entity.DishFlavor;
import com.ybl.entity.Seats;
import com.ybl.service.SeatsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 餐位管理 前端控制器
 * </p>
 *
 * @author ybl
 * @since 2023-12-14
 */

@Slf4j
@RestController
@RequestMapping("/seats")
public class SeatsController {
    @Autowired
    private SeatsService seatsService;

    /**
     * 餐位信息分页查询
     */
    @GetMapping("/page")
    @Cacheable(value = "seatsCache", key = "'seats' + '_' + #page + '_' + #pageSize + #name")
    public R<Page<Seats>> page(Integer page, Integer pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        Page<Seats> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Seats> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Seats::getName, name);
        queryWrapper.orderByDesc(Seats::getUpdateTime);
        seatsService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 新增餐位
     */
    @PostMapping
    @CacheEvict(value = "seatsCache", allEntries = true)
    public R<String> save(@RequestBody Seats seats) {
        log.info("seats:{}", seats.toString());
        seatsService.save(seats);
        return R.success("新增餐位成功");
    }

    /**
     * 根据id查询餐位信息
     */
    @GetMapping("/{id}")
    public R<Seats> get(@PathVariable Long id) {
        Seats seats = seatsService.getById(id);
        return R.success(seats);
    }

    /**
     * 修改餐位
     */
    @PutMapping
    @CacheEvict(value = "seatsCache", allEntries = true)
    public R<String> update(@RequestBody Seats seats) {
        log.info("seats:{}", seats.toString());
        final boolean b = seatsService.updateById(seats);
        return R.success("修改餐位成功");
    }
    /**
     * 删除餐位
     */
    @DeleteMapping
    @CacheEvict(value = "seatsCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("删除餐位，id为：{}", ids);
        final boolean b = seatsService.removeByIds(ids);
        return R.success("删除餐位删除成功");
    }

    /**
     * 更改餐位状态
     * 1：启用
     * 0：停用
     */
    @PostMapping("/status/{state}")
    @CacheEvict(value = "seatsCache", allEntries = true)
    public R<String> status(@PathVariable Byte state, @RequestParam List<Long> ids) {
        log.info("更改餐位状态，id为：{} ,操作为：{}", ids, state);
        LambdaQueryWrapper<Seats> DQW = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            DQW.eq(Seats::getId, id);
            DQW.or();
        }
        Seats seats = new Seats();
        seats.setCwstatus(Integer.valueOf(state));
        seatsService.update(seats, DQW);
        return R.success("餐位状态已经更改成功！");
    }

    /**
     * 获取空闲餐位信息
     */
    @GetMapping("/list")
    public R<List<Seats>> list() {
        List<Seats> list;
        LambdaQueryWrapper<Seats> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Seats::getSystatus,0);
        queryWrapper.orderByDesc(Seats::getUpdateTime);
        list = seatsService.list(queryWrapper);
        return R.success(list);
    }
}
